/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.lcm.metadata.core.util;

import com.inspur.edp.lcm.metadata.api.exception.ErrorCodes;
import com.inspur.edp.lcm.metadata.api.exception.LcmMetadataDevException;
import com.inspur.edp.lcm.metadata.api.mvnEntity.MavenPackageRefs;
import com.inspur.edp.lcm.metadata.common.Utils;
import org.springframework.util.StringUtils;

import java.util.Arrays;

public class MdpkgUtils {

    public static MavenPackageRefs getGAByMdpkgName(String mdpkgName) {
        return getGAByMdpkgNameAndPackagePrefix(mdpkgName, "");
    }

    public static MavenPackageRefs getGAByMdpkgNameWithApi(String mdpkgName) {
        MavenPackageRefs mavenPackageRef = getGAByMdpkgName(mdpkgName);
        mavenPackageRef.setArtifactId(mavenPackageRef.getArtifactId() + "-api");
        return mavenPackageRef;
    }

    public static MavenPackageRefs getGAWithApi(String mdpkgName, String packagePrefix) {
        MavenPackageRefs mavenPackageRefs = getGAByMdpkgNameAndPackagePrefix(mdpkgName, packagePrefix);
        mavenPackageRefs.setArtifactId(mavenPackageRefs.getArtifactId() + "-api");
        return mavenPackageRefs;
    }

    private static MavenPackageRefs getGAByMdpkgNameAndPackagePrefix(String mdpkgName, String packagePrefix) {
        MavenPackageRefs mavenPackageRef = new MavenPackageRefs();

        // 提前
        if ("Inspur.Gsp.Common.CommonCmp.mdpkg".equals(mdpkgName)) {
            mavenPackageRef.setGroupId("com.inspur.gsp");
            mavenPackageRef.setArtifactId("gsp-common-commoncomponent");
            mavenPackageRef.setMetadataPackageName(mdpkgName);
            return mavenPackageRef;
        }
        if ("Inspur.Gsp.Common.CommonUdt.mdpkg".equals(mdpkgName)) {
            mavenPackageRef.setGroupId("com.inspur.gsp");
            mavenPackageRef.setArtifactId("gsp-pfcommon-commonudt");
            mavenPackageRef.setMetadataPackageName(mdpkgName);
            return mavenPackageRef;
        }

        if (!StringUtils.hasLength(mdpkgName)) {
            throw new LcmMetadataDevException(ErrorCodes.ECP_METADATA_DEV_0005, "mdpkgName");
        }
        if (!StringUtils.hasLength(packagePrefix)) {
            packagePrefix = Utils.DEFAULT_PACKAGE_PREFIX;
        }
        String groupId;
        String artifactId;

        String[] info = mdpkgName.split("\\.");
        switch (info.length) {
            case 1:
                // Name -> com.name | name
                groupId = packagePrefix + Utils.DOT + info[0].toLowerCase();
                artifactId = String.format("%s", info[0]).toLowerCase();
                break;
            case 2:
                // Inspur.Name -> com.inspur | name
                groupId = packagePrefix + Utils.DOT + info[0].toLowerCase();
                artifactId = String.format("%s", info[1]).toLowerCase();
                break;
            case 3:
                // Inspur.GS.Name -> com.inspur.gs | gs-name
                groupId = String.format(packagePrefix + Utils.DOT + "%s" + Utils.DOT + "%s", info[0], info[1]).toLowerCase();
                artifactId = String.format("%s-%s", info[1], info[2]).toLowerCase();
                break;
            case 6:
                if ("Front".equals(info[5])) {
                    groupId = String.format(packagePrefix + Utils.DOT + "%s" + Utils.DOT + "%s", info[0], info[1]).toLowerCase();
                    artifactId = String.format("%s-%s-%s", info[2], info[3], info[4] + info[5]).toLowerCase();
                    break;
                }
            default:
                // Inspur.GS.P1.P2....PN -> com.inspur.gs | p1-p1-...-pn
                groupId = String.format(packagePrefix + Utils.DOT + "%s" + Utils.DOT + "%s", info[0], info[1]).toLowerCase();
                String[] strings = Arrays.copyOfRange(info, 2, info.length);
                artifactId = String.join("-", strings).toLowerCase();
        }
        mavenPackageRef.setGroupId(groupId);
        mavenPackageRef.setArtifactId(artifactId);
        mavenPackageRef.setMetadataPackageName(mdpkgName);
        return mavenPackageRef;
    }

}
